﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Utility.Http;
using Ecreate.Model;
using Ecreate.Business;

namespace Ecreate.Web.systemanage.product
{
    public partial class productcrud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.classid.Value = Request.QueryString["classid"];
                this.actiontype.Value = Request.QueryString["type"];

                if (this.actiontype.Value == "edit")
                {
                    #region MyRegion
                    string pid = Request.QueryString["pid"];
                    ProductService service = new ProductService();
                    Product model = new Product();
                    try
                    {
                        model.Pid = int.Parse(this.Request.QueryString["pid"]);
                        model = service.getProductByID(model);

                        this.imageurl.Value = model.Pimageurl;
                        this.pid.Value = model.Pid.ToString();
                        this.pname.Text = model.Pname;
                        this.pdescription.Text = model.Pdescription;
                        this.classid.Value = model.Classid;
                        if (model.Pimageurl == "")
                        {
                            this.Image1.ImageUrl = "/content/images/Chart/NoData.jpg";
                        }
                        else
                        {
                            this.Image1.ImageUrl = model.Pimageurl;
                        }
                        return;
                    }
                    catch
                    {
                        string str_alert = "<script type=\"text/javascript\" language=\"javascript\">           alert('不存在该产品,请刷新显示数据!')　　; </script>";
                        string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + this.classid.Value + "\"　　; </script>";
                        Response.Write(str_alert);
                        Response.Write(str_url);
                        return;
                    } 
                    #endregion
                }
                if (this.actiontype.Value == "delete")
                {
                    #region MyRegion
                    try
                    {

                        ProductService service = new ProductService();
                        Product model = new Product();
                        model.Pid = int.Parse(this.Request.QueryString["pid"]);
                        service.Delete(model);
                        string str_alert = "<script type=\"text/javascript\" language=\"javascript\">          alert('删除成功')　　; </script>";
                        string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + this.classid.Value + "\"　　; </script>";
                        Response.Write(str_alert);
                        Response.Write(str_url);
                        return;
                    }
                    catch
                    {
                        string str_alert = "<script type=\"text/javascript\" language=\"javascript\">           alert('不存在该产品,请刷新显示数据!')　　; </script>";
                        string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + this.classid.Value + "\"　　; </script>";
                        Response.Write(str_alert);
                        Response.Write(str_url);
                        return;
                    } 
                    #endregion
                   
                }

            }
            string uploadflag = Request.Form["uploadflag"];
            if (!String.IsNullOrEmpty(uploadflag))
            {
                #region MyRegion
                if ("upload".Equals(uploadflag))
                {
                    string filename = this.filename.Value;
                    if (System.IO.File.Exists(filename))
                    {
                        FileUpLoad f = FileUpLoad.UpLoadFile(this.filename, FileUpLoad.ImagePath, filename, true);
                        this.Image1.ImageUrl = f.FilePath;
                        this.imageurl.Value = f.FilePath;
                    }
                    this.uploadflag.Value = "";
                    this.pdescription.Text = Request.Form["pdescription"];
                    this.pname.Text = Request.Form["pname"];
                    this.classid.Value = Request.Form["classid"];
                    return;
                } 
                #endregion
            }
            string action = Request.Form["actiontype"];
            if (!String.IsNullOrEmpty(action))
            {
                #region MyRegion
                if ("edit".Equals(action))
                {
                    ProductService service = new ProductService();
                    Product model = new Product();
                    model.Classid = this.Request.Form["classid"].ToString();
                    model.Pdescription = this.Request.Form["pdescription"];
                    model.Pname = this.Request.Form["pname"];
                    model.Pimageurl = this.Request.Form["imageurl"];
                    model.Pid = int.Parse(this.Request.Form["pid"]);
                    service.Update(model);
                }
                else if ("insert".Equals(action))
                {
                    ProductService service = new ProductService();
                    Product model = new Product();
                    model.Classid = this.Request.Form["classid"].ToString();
                    model.Pdescription = this.Request.Form["pdescription"];
                    model.Pname = this.Request.Form["pname"];
                    model.Pimageurl = this.Request.Form["imageurl"];
                    service.Insert(model);
                }
                string str_alert = "<script type=\"text/javascript\" language=\"javascript\">          alert('保存成功')　　; </script>";
                string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + this.classid.Value + "\"　　; </script>";
                Response.Write(str_alert);
                Response.Write(str_url); 
                #endregion
            }
        }
    }
}
